//
//  RootView.m
//  UI6_UIScrollView_UIPageControl
//
//  Created by dllo on 15/11/13.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "RootView.h"

@interface RootView ()<UIScrollViewDelegate>

@property (nonatomic, retain) UIScrollView *scrollV;
@property (nonatomic, retain) UIPageControl *pageC;

@end

@implementation RootView

- (void)dealloc
{
    [_scrollV release];
    [_pageC release];
    [super dealloc];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubviews];
    }
    return self;
}

- (void)createSubviews
{
    self.backgroundColor = [UIColor whiteColor];
//    UIPageControl
//    UIPageControl *pageC = [[UIPageControl alloc] initWithFrame:CGRectMake(40, 100, 300, 100)];
//    
//    pageC.numberOfPages = 19;
//    
//    // 未被选择点的颜色
//    pageC.pageIndicatorTintColor = [UIColor blueColor];
//    
//    // 选择点的颜色
//    pageC.currentPageIndicatorTintColor = [UIColor redColor];
//    
//    pageC.backgroundColor = [UIColor blackColor];
//    [pageC addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventValueChanged];
//    [self addSubview:pageC];
//    [pageC release];

    
    self.scrollV = [[UIScrollView alloc] initWithFrame:self.frame];
    
#pragma 重要属性
    // 19个与屏幕等宽的显示视图
    // 高度0为默认与scrollV等高
    self.scrollV.contentSize = CGSizeMake(self.frame.size.width * 21, 0);
    
    // 按页切换
    self.scrollV.pagingEnabled = YES;
    self.scrollV.delegate = self;
    
    // 边界反弹效果(第一张和最后一张)
    self.scrollV.bounces = YES;
    
    // 是否允许滑动
//    self.scrollV.scrollEnabled = NO;
    
    // 是否显示滑动条(水平时)
    self.scrollV.showsHorizontalScrollIndicator = YES;
    
    // 是否显示滑动条(垂直时)
    self.scrollV.showsVerticalScrollIndicator = YES;
    
#pragma 实用属性
    // 设置显示哪页(也可用来读取用户操作到哪页)
    // contentOffset : 偏移量
    self.scrollV.contentOffset = CGPointMake(self.frame.size.width, 0);
    
    
#pragma 缩放 - 要想实现缩放功能需要设置两处:1, 最值比例; 2, 实现协议方法告诉系统哪个页面允许缩放
    // 最小/最大缩放比例
//    self.scrollV.minimumZoomScale = 0.3;
//    self.scrollV.maximumZoomScale = 10;
    
    
    [self addSubview:self.scrollV];
    [self.scrollV release];
    
    
    for (NSInteger i = 0; i < 21; i++) {
        
        // 两个问题:1, 坐标如何写; 2, 图片名如何找.
        
#pragma 解决缩放问题 - 在self.scrollV与imageView之间添加数个小scrollView专门缩放
        UIScrollView *subScrollV = [[UIScrollView alloc] initWithFrame:CGRectMake(self.scrollV.frame.size.width * i, 0, self.scrollV.frame.size.width, self.scrollV.frame.size.height)];
        subScrollV.minimumZoomScale = 0.5;
        subScrollV.maximumZoomScale = 10;
        
        subScrollV.delegate = self;
        
        [self.scrollV addSubview:subScrollV];
        [subScrollV release];
        
        
        NSString *fileName = nil;
        
        if (0 == i) {
            fileName = @"image19.jpg";
        } else if (20 == i) {
            fileName = @"image1.jpg";
        } else {
            fileName = [NSString stringWithFormat:@"image%ld.jpg", i];
        }
        
        
        
        UIImageView *imageV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
//        NSString *fileName = [NSString stringWithFormat:@"image%ld.jpg", (i + 1)];
        imageV.image = [UIImage imageNamed:fileName];
        [subScrollV addSubview:imageV];
        [imageV release];
        
        
    }
    
    
    self.pageC = [[UIPageControl alloc] initWithFrame:CGRectMake(50, 500, self.frame.size.width - 50 * 2, 30)];
    
    self.pageC.numberOfPages = 21;
    self.pageC.backgroundColor = [UIColor blackColor];
    [self.pageC addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventValueChanged];
    [self addSubview:self.pageC];
    [_pageC release];
    
    
    
    
    
    
    
    
    
//    UIImageView *imageV1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
//    imageV1.image = [UIImage imageNamed:@"image1.jpg"];
//    [scrollV addSubview:imageV1];
//    [imageV1 release];
//    
//    UIImageView *imageV2 = [[UIImageView alloc] initWithFrame:CGRectMake(self.frame.size.width, 0, self.frame.size.width, self.frame.size.height)];
//    imageV2.image = [UIImage imageNamed:@"image2.jpg"];
//    [scrollV addSubview:imageV2];
//    [imageV2 release];

}

/*************************缩放相关*********************************/

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    NSLog(@"返回允许缩放的视图");
    return scrollView.subviews.firstObject;
}

- (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(UIView *)view
{
    NSLog(@"缩放将要开始");
}

- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale
{
    NSLog(@"缩放已将结束");
}

- (void)scrollViewDidZoom:(UIScrollView *)scrollView
{
    NSLog(@"缩放时触发");
}

/***********************缩放相关****************************************/

#pragma 拖拽的响应过程 - 拖拽将要开始 -> 拖拽将要结束 -> 拖拽已经结束 -> 减速将要开始 -> 减速已经结束

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    NSLog(@"拖拽时触发");
}


- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    NSLog(@"拖拽将要开始");
}

- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset NS_AVAILABLE_IOS(5_0)
{
    NSLog(@"拖拽将要结束");
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    NSLog(@"拖拽已经结束");
}

- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
{
    NSLog(@"减速将要开始");
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSLog(@"减速已经结束");
    
#pragma 点随页动 - 通过scrollView的偏移量获取并计算页面下标
    NSInteger index = scrollView.contentOffset.x / scrollView.frame.size.width;
//    self.pageC.currentPage = index;
    if (0 == index) {
        self.scrollV.contentOffset = CGPointMake(self.frame.size.width * 19, 0);
    }
    if (20 == index) {
        self.scrollV.contentOffset = CGPointMake(self.frame.size.width * 1, 0);
    }
}


- (void)pageAction:(UIPageControl *)sender
{
//    NSLog(@"当前选择的页码:%ld", sender.currentPage);
    
#pragma 页随点动 - 将scrollView的contentOffset的偏移量与pageControl的当前那点关联
    self.scrollV.contentOffset = CGPointMake(self.scrollV.frame.size.width*sender.currentPage, 0);
    
}


@end
